<?php

use Illuminate\Database\Seeder;

class UserBonusCountTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return string
     */
    public function run()
    {

//        $commission_log = DB::table("commission_log as t1")
//            ->leftJoin ("users as t2","t1.user_id","=","t2.id")
//            ->select ("t1.*", "t2.type as user_type")
//            ->where ("t1.user_id", "<>", 1)
//            ->whereNotIn("t2.type",[3,4])
//            ->toSql ();
//        var_dump(($commission_log));die;
        // 查询会员参加活动的
        $user_data_av = DB::table("commission_log as t1")
            ->leftJoin ("users as t2","t1.user_id","=","t2.id")
            ->select("t1.*", "t2.type as user_type")
            ->where("t1.type","=",1)
            ->where("t2.type", "=", 4)
            ->get();
//        var_dump (count ($user_data_av));

        // 业务员参加活动
        $salesman_data_av = DB::table("commission_log as t1")
            ->leftJoin ("users as t2","t1.user_id","=","t2.id")
            ->select("t1.*", "t2.type as user_type")
            ->where("t1.type","=",1)
            ->where("t2.type", "=", 3)
            ->get();
//        var_dump (count ($salesman_data_av));
        //会员参加分销
        $user_dis = DB::table("commission_log as t1")
            ->leftJoin ("users as t2","t1.user_id","=","t2.id")
            ->select("t1.*", "t2.type as user_type")
            ->where("t1.type","=",0)
            ->where("t2.type", "=", 4)
            ->get();
//        var_dump (count ($user_dis));
        // 业务员参加分销
        $salesman_dis = DB::table("commission_log as t1")
            ->leftJoin ("users as t2","t1.user_id","=","t2.id")
            ->select("t1.*", "t2.type as user_type")
            ->where("t1.type","=",0)
            ->where("t2.type", "=", 3)
            ->get();
//        var_dump (count ($salesman_dis));

        // 运营商参加分销
        $operator_dis = DB::table("commission_log as t1")
            ->leftJoin ("users as t2","t1.user_id","=","t2.id")
            ->select("t1.*", "t2.type as user_type")
            ->where("t1.type","=",0)
            ->where("t2.type", "=", 2)
            ->get();

        $place_partner =  DB::table("commission_log as t1")
            ->leftJoin ("users as t2","t1.user_id","=","t2.id")
            ->select("t1.*", "t2.type as user_type")
            ->where("t1.type","=",0)
            ->where("t2.type", "=", 1)
            ->get();

        foreach ($user_data_av as $item){
            // 拉新记录
            $user_invite_one_yuan = new \App\Model\UserInviteOneYuan();
            $user_invite_one_yuan->title = isset($item->title) ?$item->title :'';
            $user_invite_one_yuan->user_id = $item->user_id;
            $user_invite_one_yuan->content = "";
            $user_invite_one_yuan->money = $item->amount;
            $user_invite_one_yuan->created_at = $item->created_at;
            $user_invite_one_yuan->save();
            //
            $user_bonus_count = new \App\Model\UserBonusCount();
            $user_bonus_count->type = 2;
            $user_bonus_count->income_type= 1;
            $user_bonus_count->money = 1;
            $user_bonus_count->relation_id = $user_invite_one_yuan->id;
            $user_bonus_count->user_id = $item->user_id;
            $user_bonus_count->created_at = $item->created_at;
            $user_bonus_count->save();
        }


        foreach ($salesman_data_av as $item){
            // 业务员
            $user_salesman_invite_one_yuan = new \App\Model\UserSalesmanInviteOneYuan();
            $user_salesman_invite_one_yuan->title = isset($item->title) ?$item->title :'';
            $user_salesman_invite_one_yuan->user_id = $item->user_id;
            $user_salesman_invite_one_yuan->content = "";
            $user_salesman_invite_one_yuan->money = $item->amount;
            $user_salesman_invite_one_yuan->created_at = $item->created_at;
            $user_salesman_invite_one_yuan->save();
            //
            $user_salesman_bonus_count = new \App\Model\UserSalesmanBonusCount();
            $user_salesman_bonus_count->type = 2;
            $user_salesman_bonus_count->income_type= 1;
            $user_salesman_bonus_count->money = 1;
            $user_salesman_bonus_count->relation_id = $user_salesman_invite_one_yuan->id;
            $user_salesman_bonus_count->user_id = $item->user_id;
            $user_salesman_bonus_count->created_at = $item->created_at;
            $user_salesman_bonus_count->save();
        }

        foreach ($user_dis as $item){
            $user_bonus_count = new \App\Model\UserBonusCount();
            $user_bonus_count->type = 1;
            $user_bonus_count->income_type= 1;
            $user_bonus_count->money = 1;
            $user_bonus_count->relation_id = $item->cd_id;
            $user_bonus_count->user_id = $item->user_id;
            $user_bonus_count->created_at = $item->created_at;
            $user_bonus_count->save();
        }

        foreach ($salesman_dis as $item){
            $user_salesman_bonus_count = new \App\Model\UserSalesmanBonusCount();
            $user_salesman_bonus_count->type = 1;
            $user_salesman_bonus_count->income_type= 1;
            $user_salesman_bonus_count->money = 1;
            $user_salesman_bonus_count->relation_id = $item->cd_id;
            $user_salesman_bonus_count->user_id = $item->user_id;
            $user_salesman_bonus_count->created_at = $item->created_at;
            $user_salesman_bonus_count->save();
        }

        foreach ($operator_dis as  $item){
            $operators_bonus_count = new \App\Model\UserOperatorsBonusCount();
            $operators_bonus_count->type = 1;
            $operators_bonus_count->income_type = 1;
            $operators_bonus_count->user_id = $item->user_id;
            $operators_bonus_count->money = $item->amount;
            $operators_bonus_count->relation_id = $item->cd_id;
            $operators_bonus_count->created_at=$item->created_at;
            $operators_bonus_count->save();
        }
        foreach ($place_partner as  $item){
            $operators_bonus_count = new \App\Model\UserPlacePartnerBonusCount();
            $operators_bonus_count->type = 1;
            $operators_bonus_count->income_type = 1;
            $operators_bonus_count->user_id = $item->user_id;
            $operators_bonus_count->money = $item->amount;
            $operators_bonus_count->relation_id = $item->cd_id;
            $operators_bonus_count->created_at=$item->created_at;
            $operators_bonus_count->save();
        }

        return "更新成功！";
    }
}
